home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_pcdp / adas / pca.ada < prev    next >
Text File  |  1996-01-30  |  1KB  |  66 lines

  1. with Text_IO; use Text_IO;
  2. procedure sel is
  3.  
  4.   N: constant := 10;
  5.  
  6.   task type Buffer is
  7.     entry Append(I: in  Integer);
  8.     entry Take  (I: out Integer);
  9.   end Buffer;
  10.  
  11.   task body Buffer is
  12.     B: array(0..N) of Integer;
  13.     In_Ptr, Out_Ptr: Integer := 0;
  14.     Count: Integer := 0;
  15.  
  16.   begin
  17.     loop
  18.       select
  19.         when Count < N =>
  20.           accept Append(I: in Integer) do
  21.             B(In_Ptr) := I;
  22.           end Append;
  23.         Count := Count + 1;
  24.         In_Ptr := (In_Ptr + 1) mod N;
  25.       or
  26.         when Count > 0 =>
  27.          accept Take(I: out Integer) do
  28.            I := B(Out_Ptr);
  29.           end Take;
  30.         Count := Count - 1;
  31.         Out_Ptr := (Out_Ptr + 1) mod N;
  32.       or
  33.         terminate;
  34.       end select;
  35.     end loop;
  36.   end Buffer;
  37.  
  38.   task Producer;
  39.   task body Producer is
  40.     N: Integer := 0;
  41.   begin
  42.     for I in 1..25 loop
  43.       N := N + 1;
  44.       Put("Produce  ");
  45.       Put(N);
  46.       New_Line;
  47.       Buffer.Append(N);
  48.     end loop;
  49.   end Producer;
  50.  
  51.   task Consumer;
  52.   task body Consumer is
  53.     N: Integer;
  54.   begin
  55.     for I in 1..25 loop
  56.       Buffer.Take(N);
  57.       Put("Consume   ");
  58.       Put(N);
  59.       new_line;
  60.     end loop;
  61.   end Consumer;
  62.  
  63. begin
  64.   Put_Line("Main program");
  65. end sel;
  66.